//
//  NumberPadView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/4/21.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class NumberPadView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {

    var currentNumber : Int = 0
    private var collection : UICollectionView?
    func scrool(destinatedNum : Int) -> Void {
        if let currentIdx = collection!.indexPathsForVisibleItems.first {
            if currentIdx.row % 10000 > destinatedNum {
                let delt = destinatedNum + 10 - currentIdx.row % 10000
                let idx = IndexPath.init(row: currentIdx.row + delt, section: 0)
                collection?.scrollToItem(at: idx, at: .bottom, animated: true)
            } else {
                let delt = destinatedNum - currentIdx.row % 10000
                let idx = IndexPath.init(row: currentIdx.row + delt, section: 0)
                collection?.scrollToItem(at: idx, at: .bottom, animated: true)
            }
            currentNumber = destinatedNum
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = frame.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .vertical
        collection = UICollectionView.init(frame: self.bounds, collectionViewLayout: layout)
        collection!.isPagingEnabled = true
        collection!.delegate = self
        collection!.isUserInteractionEnabled = false
        collection!.dataSource = self
        collection!.showsVerticalScrollIndicator = false
        collection!.backgroundColor = UIColor.clear
        collection!.register(UINib.init(nibName: "NumberPadCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: "numberpad")
        self.addSubview(collection!)
        collection!.setContentOffset(CGPoint.init(x: 0, y: 5000 * frame.size.height), animated: false)
    }
    
    //delegate and datasource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10000
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "numberpad", for: indexPath) as? NumberPadCollectionCell {
            cell.number = String(indexPath.row % 10)
            return cell
        }
        return UICollectionViewCell.init()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}
